package com.zzyl.serve.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDTO;
import com.zzyl.serve.service.INursingProjectPlanService;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private INursingProjectPlanService iNursingProjectPlanService;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id) {
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);
        List<NursingProjectPlan> list = iNursingProjectPlanService.list(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        if (list != null) {
            List<NursingProjectPlanVo> targetList = list.stream().map(nursingProjectPlan -> {
                NursingProjectPlanVo nursingProjectPlanVo = new NursingProjectPlanVo();
                nursingProjectPlanVo.setProjectId(String.valueOf(nursingProjectPlan.getProjectId()));
                nursingProjectPlanVo.setId(Long.valueOf(nursingProjectPlan.getId()));
                nursingProjectPlanVo.setPlanId(Long.valueOf(nursingProjectPlan.getPlanId()));
                nursingProjectPlanVo.setExecuteCycle(String.valueOf(nursingProjectPlan.getExecuteCycle()));
                nursingProjectPlanVo.setExecuteTime(String.valueOf(nursingProjectPlan.getExecuteTime()));
                nursingProjectPlanVo.setExecuteFrequency(Long.valueOf(nursingProjectPlan.getExecuteFrequency()));
                return nursingProjectPlanVo;
            }).collect(Collectors.toList());
            nursingPlanVo.setProjectPlans(targetList);
        }
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * 
     * @param dto 护理计划
     * @return 结果
     */
    @Transactional
    @Override
    public int insertNursingPlan(NursingPlanDTO dto) {
        /*
         * 新增护理计划逻辑
         * 涉及到两张表的增加
         * 1,新增护理计划表
         * 2,新增护理计划与护理项目的关系表
         * */
        //将传进来的dto转成NursingPlan对象
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto, nursingPlan);
        nursingPlan.setCreateTime(DateUtils.getNowDate());
        boolean save = this.save(nursingPlan);
        //批量保存护理项目计划关系表
        List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
        if (projectPlans != null) {
            for (NursingProjectPlan projectPlan : projectPlans) {
                projectPlan.setPlanId(nursingPlan.getId());
            }
        }
        iNursingProjectPlanService.saveBatch(projectPlans);
        return save ? 1 : 0;
    }
    /**
     * 修改护理计划
     * 
     * @param nursingPlanDTO 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int updateNursingPlan(NursingPlanDTO nursingPlanDTO) {
        /*
         *业务逻辑分析
         * 1,接收前端传进来的参数dto
         * 2,创建一个NursingPlan对象拷贝dto中的数据
         * 3,判断当前对象中是否有护理项目集合数据:有,则删除原有的集合数据;没有,则只修改计划数据
         *  */
        NursingPlan nursingPlan = new NursingPlan();
        nursingPlan.setPlanName(nursingPlanDTO.getPlanName());
        nursingPlan.setId(nursingPlanDTO.getId());
        nursingPlan.setStatus(nursingPlanDTO.getStatus());
        nursingPlan.setSortNo(nursingPlanDTO.getSortNo());
        //存储数据
        boolean save = this.update(nursingPlan,Wrappers.<NursingPlan>lambdaUpdate().eq(NursingPlan::getId, nursingPlan.getId()));
        //判断集合是否有数据
        if (nursingPlanDTO.getProjectPlans() != null || !nursingPlanDTO.getProjectPlans().isEmpty()) {
            //先删除
            iNursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, nursingPlan.getId()));
            //再添加
            List<NursingProjectPlan> projectPlans = nursingPlanDTO.getProjectPlans();
            for (NursingProjectPlan projectPlan : projectPlans) {
                projectPlan.setPlanId(nursingPlanDTO.getId());
                iNursingProjectPlanService.save(projectPlan);
            }
        }
        return save ? 1 : 0;
    }
    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }
}
